package com.share.config.swagger;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Administrator
 * @data 2019/6/9
 */

/*常用注解：
@Api()用于类  -swagger标记扫描
@ApiOperation() -请求方法说明
@ApiImplicitParams()  方法参数s
@ApiImplicitParam() 方法参数
@ApiParam() --请求方法的参数绑定说明--一般不用

@ApiModel()  model类标记说明
@ApiModelProperty() model类属性*/

/*@EnableSwagger2---这个注解会自动加载默认配置,请别放在springboot启动类处*/

@Configuration
@EnableSwagger2
@Profile({"dev","test"})// 设置 dev test 环境开启
public class SwaggerConfig {

    private static String basePackage ="com.szeport.dyck";
    private static String sessionId="JdWksfhH-zX3uiZnCjeoP";

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
//                .enable(true)//启用swagger显示
                .apiInfo(apiInfo())//设定头信息
                .globalOperationParameters(params())//设定参数
                .select()//创建api管理器
                .apis(RequestHandlerSelectors.basePackage(basePackage)) //api所在包名
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))/*方法加入文档的标记注解--默认会加入*/
                .paths(PathSelectors.any())//路径赛选
                .build()
                .protocols( protocols())/*设置http或者https协议*/
                ;
    }

    private Set<String> protocols(){
        Set set = new HashSet();
        set.add("http");
        set.add("https");
        return set;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("深圳单一窗口api")
                .description("SpringBoot2.1.5 API接口文档")
                .version("2.1.0")
                .build();
    }

    private List<Parameter> params() {
        List<Parameter> pars = new ArrayList<>();

        /*header处添加sessionId参数*/
        pars.add(new ParameterBuilder()
                .name("sessionId")
                .description("前端请跳过该参数-sessionId")
                .modelRef(new ModelRef("string"))
                .defaultValue(sessionId)//设定默认值
                .parameterType("header")
                .required(true)
                .build()
        );

        return pars;
    }
}


